from django.contrib.auth.models import User as AuthUser
from myhomework.models import *
from myhomework.exceptions.exception import *
from django.contrib.auth.models import Permission
from myhomework.utils import get_param
from django.http import HttpResponse
from django.utils.decorators import available_attrs



class SettingDao(object):
    
    settings = None 
    #Settings(allow_register = True, allow_create_group = True, allow_join_group = True, number_of_group_member = 4,user_number_per_page = 6, group_number_per_page = 10)
    
    @staticmethod
    def update_settings(new_settings):
        new_settings.id = SettingDao.get_settings().id
        SettingDao.settings = new_settings
        SettingDao.settings.save()
    
    @staticmethod
    def get_setting(attribute):
        
        """
        memory is none
        """ 
        if( not SettingDao.settings ):
            SettingDao.settings = Settings.objects.all()
            
            """
            database is nune
            """
            if( not SettingDao.settings ):
                SettingDao.settings = Settings(allow_register = True,
                                               allow_create_group = True,
                                               allow_join_group = True, 
                                               allow_edit_group=True,
                                               allow_quit_group=True,
                                               allow_delete_group = True,
                                               number_of_group_member = 4,
                                               user_number_per_page = 6, 
                                               group_number_per_page = 10,
                                               root_path_of_homework = 'myhomework/all_homework_submited',
                                               root_path_of_src_file = 'myhomework/all_src_file',
                                               homework_number_per_page = 5)
                SettingDao.settings.save()
            
            else:
                SettingDao.settings = SettingDao.settings[0]
            
        tmp = getattr(SettingDao.settings, attribute)   
             
        return tmp
    
    @staticmethod
    def get_settings():
        
        """
        memory is none
        """ 
        if( not SettingDao.settings ):
            SettingDao.settings = Settings.objects.all()
            
            """
            database is nune
            """
            if( not SettingDao.settings ):
                SettingDao.settings = Settings(allow_register = True,
                                               allow_create_group = True,
                                               allow_join_group = True, 
                                               allow_edit_group=True,
                                               allow_quit_group=True,
                                               allow_delete_group = True,
                                               number_of_group_member = 4,
                                               user_number_per_page = 6, 
                                               group_number_per_page = 10,
                                               homework_number_per_page = 5,
                                               root_path_of_homework = 'myhomework/all_homework_submited',
                                               root_path_of_src_file = 'myhomework/all_src_file'
                                               )
                SettingDao.settings.save()
            
            else:
                SettingDao.settings = SettingDao.settings[0]
            
        return SettingDao.settings
        
    
    @staticmethod
    def set_setting(attribute, value):
        
        if( not SettingDao.settings ):
            SettingDao.settings = Settings.objects.all()
            
            if( not SettingDao.settings ):
                SettingDao.settings = Settings(allow_register = True,
                                               allow_create_group = True,
                                               allow_join_group = True, 
                                               allow_edit_group=True,
                                               allow_quit_group=True,
                                               allow_delete_group = True,
                                               number_of_group_member = 4,
                                               user_number_per_page = 6, 
                                               group_number_per_page = 10,
                                               homework_number_per_page = 5,
                                               root_path_of_homework = 'myhomework/all_homework_submited',
                                               root_path_of_src_file = 'myhomework/all_src_file')
                SettingDao.settings.save()
            
            else:
                SettingDao.settings = SettingDao.settings[0]
            
        setattr(SettingDao.settings, attribute, value) 
        SettingDao.settings.save()  
    
 
